﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:ctrl="clr-namespace:Tum.PDE.LanguageDSL.Visualization.View.Controls"
                    xmlns:selector="clr-namespace:Tum.PDE.LanguageDSL.Visualization.Selectors"
                    xmlns:vm="clr-namespace:Tum.PDE.LanguageDSL.Visualization.ViewModel.Diagram">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="DiagramItemStyle.xaml"/>
        <ResourceDictionary Source="MenuItemStyle.xaml"/>
        <ResourceDictionary Source="DiagramModelElements.xaml"/>
        <ResourceDictionary Source="DiagramAdditionalStyles.xaml"/>
        <ResourceDictionary Source="ModelDiagramStyle.xaml"/>
        <ResourceDictionary Source="SpecificElementsDiagramTemplate.xaml"/>        
    </ResourceDictionary.MergedDictionaries>

    <!-- Style for embedding diagram items control. -->
    <Style TargetType="{x:Type ContentControl}" x:Key="EmbeddingDiagramNodeStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ContentControl}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>

                        <!-- Connecting Lines -->
                        <Rectangle x:Name="VerLn" Width="1" Stroke="Black" Grid.Column="0" Margin="70,0,0,0" Grid.RowSpan="2" SnapsToDevicePixels="true"/>

                        <ContentPresenter Margin="0,0,0,0" x:Name="ItemsHost" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"/>
                    </Grid>

                    <ControlTemplate.Triggers>
                        <!-- This trigger changes the connecting lines if the item is the last in the list -->
                        <DataTrigger Binding="{Binding Path=IsLastNode}" Value="true">
                            <Setter TargetName="VerLn" Property="Height" Value="30"/>
                            <Setter TargetName="VerLn" Property="VerticalAlignment" Value="Top"/>
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>    
    
    <!-- Template for displaying embedding nodes. It also contains the corresponding connections. -->
    <DataTemplate x:Key="EmbeddingDiagramNodesTemplate">
        <ContentControl Style="{StaticResource EmbeddingDiagramNodeStyle}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <!-- Connection. -->
                <Canvas Grid.Column="0">
                    <Path Name="PART_ConnectionPath"
                              StrokeThickness="1"
                              Stroke="Black"
                              StrokeStartLineCap="Round"
                              StrokeEndLineCap="Round"
                              StrokeLineJoin="Round"
                              SnapsToDevicePixels="True">
                        <Path.Data>
                            <PathGeometry>
                                <PathFigure StartPoint ="0, 30">
                                    <LineSegment Point="70, 30" />
                                </PathFigure>
                            </PathGeometry>
                        </Path.Data>
                    </Path>
                </Canvas>

                <StackPanel Orientation="Vertical" Grid.Column="1" VerticalAlignment="Top">
                    <ctrl:DesignerItem Margin="10,0,0,0" HorizontalAlignment="Left" Style="{StaticResource ShapeClassElementStyle}"/>
                    <ContentControl Style="{DynamicResource DiagramEmbeddingItemsControlStyle}" />
                </StackPanel>
            </Grid>
        </ContentControl>
    </DataTemplate>

    <!-- Diagram Embedding elements ItemsControl style.-->
    <Style TargetType="{x:Type ContentControl}" x:Key="DiagramEmbeddingItemsControlStyle">
        <Style.Resources>
            <!-- Connection symbols. -->
            <Style x:Key="DiamondSymbolBaseStyle" TargetType="Path">
                <Setter Property="Fill" Value="#888"/>
                <Setter Property="Stretch" Value="Fill"/>
            </Style>
            <Style x:Key="ArrowSymbolBaseStyle" TargetType="Path">
                <Setter Property="Fill" Value="#FFF"/>
                <Setter Property="Stroke" Value="Black" />
                <Setter Property="StrokeThickness" Value="0.6" />
                <Setter Property="Stretch" Value="Fill"/>
            </Style>

            <Style x:Key="Arrow" TargetType="Path" BasedOn="{StaticResource ArrowSymbolBaseStyle}">
                <Setter Property="Data" Value="M0,0 8,4 0,8 Z"/>
            </Style>
            <Style x:Key="Diamond" TargetType="Path" BasedOn="{StaticResource DiamondSymbolBaseStyle}">
                <Setter Property="Data" Value="M-5,0 0,-5 5,0 0,5 Z"/>
            </Style>

            <Style x:Key="ArrowGridStyle" TargetType="Grid">
                <Setter Property="Width" Value="10"/>
                <Setter Property="Height" Value="10"/>
                <Setter Property="RenderTransform">
                    <Setter.Value>
                        <TranslateTransform X="-5.5" Y="-2"/>
                    </Setter.Value>
                </Setter>
            </Style>
        </Style.Resources>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ContentControl}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <!-- Column for embedding line-->
                            <ColumnDefinition Width="38"/>
                            <!-- Column for the actual elements-->
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <!-- Row for embedding relationship-->
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>

                        <!-- Connecting Line for embedding rs -->
                        <Rectangle x:Name="VerLn_Emb" Height="30" VerticalAlignment="Top" HorizontalAlignment="Right" Width="1" Stroke="Black" Grid.Row="0" Grid.Column="0" SnapsToDevicePixels="true"/>
                        <!-- Diamond symbol representing an identification of the embedding relation-->
                        <ctrl:SymbolCanvas x:Name="Symbol_Emb"  Grid.Row="0" VerticalAlignment="Top" Grid.Column="0" HorizontalAlignment="Right">
                            <Grid Style="{StaticResource ArrowGridStyle}" SnapsToDevicePixels="true">
                                <Path Name="PART_SourceAnchorPath" Style="{StaticResource Diamond}"/>
                                <Grid.LayoutTransform>
                                    <RotateTransform Angle="90"/>
                                </Grid.LayoutTransform>
                            </Grid>
                        </ctrl:SymbolCanvas>
                        <ItemsControl Margin="-71,0,0,0" Grid.Row="0" Grid.Column="1" x:Name="DiagramItemsControlEmbeddingNodes"
                                      ItemsSource="{Binding Path=EmbeddingNodes}" ItemTemplate="{StaticResource EmbeddingDiagramNodesTemplate}"/>
                    </Grid>

                    <ControlTemplate.Triggers>
                        <!-- Triggers for hiding nodes. -->
                        <DataTrigger Binding="{Binding Path=IsChildrenTreeExpanded}" Value="False">
                            <Setter TargetName="DiagramItemsControlEmbeddingNodes" Property="Visibility" Value="Collapsed"/>
                            <Setter TargetName="VerLn_Emb" Property="Visibility" Value="Collapsed"/>
                            <Setter TargetName="Symbol_Emb" Property="Visibility" Value="Collapsed"/>
                        </DataTrigger>

                        <!-- Toggle button hiding connection line. -->
                        <DataTrigger Binding="{Binding Path=HasEmbeddingNodes}" Value="False">
                            <Setter TargetName="VerLn_Emb" Property="Visibility" Value="Collapsed"/>
                            <Setter TargetName="Symbol_Emb" Property="Visibility" Value="Collapsed"/>
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!-- Shape class template. -->
    <DataTemplate x:Key="ShapeClassTemplate">
        <Grid>
            <StackPanel Orientation="Vertical">
                <ctrl:DesignerItem Margin="10,0,0,0" HorizontalAlignment="Left" Style="{StaticResource ShapeClassElementStyle}"/>
                <ContentControl Style="{StaticResource DiagramEmbeddingItemsControlStyle}" />
            </StackPanel>
        </Grid>
    </DataTemplate>

    <!-- Relationship class style. -->
    <DataTemplate x:Key="RelationshipShapeTemplate">
        <Grid>
            <ctrl:DesignerItem Margin="10,0,0,0" HorizontalAlignment="Left" Style="{StaticResource RelationshipShapeElementStyle}"/>
        </Grid>
    </DataTemplate>

    <!-- MappingRelationship class style. -->
    <DataTemplate x:Key="MappingRelationshipShapeTemplate">
        <Grid>
            <ctrl:DesignerItem Margin="10,0,0,0" HorizontalAlignment="Left" Style="{StaticResource MappingRelationshipShapeElementStyle}"/>
        </Grid>
    </DataTemplate>

    <selector:DiagramItemTemplateSelector x:Key="DiagramItemTemplateSelector"
                                       ShapeClassTemplate="{StaticResource ShapeClassTemplate}"
                                       RelationshipClassTemplate="{StaticResource RelationshipShapeTemplate}"
                                       MappingRelationshipClassTemplate="{StaticResource MappingRelationshipShapeTemplate}"/>


    <DataTemplate DataType="{x:Type vm:DiagramClassViewModel}" x:Key="DiagramClassViewDefaultTemplate">
        <ctrl:DesignerItem x:Name="DiagramClassElement" Margin="-1,0,-1,0" HorizontalAlignment="Left">
            <Grid>
                <StackPanel Orientation="Horizontal">
                    <StackPanel>
                        <Border Margin="-1" Name="BdSelected" BorderBrush="Transparent" BorderThickness="1.2" Padding="1" SnapsToDevicePixels="True">
                            <Border Name="Bd" Margin="0" BorderBrush="#FF5D5A55" BorderThickness="0.6,0" Padding="0" SnapsToDevicePixels="True">
                                <Border.Background>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="White" Offset="0"/>
                                        <GradientStop Color="#22E2DBDB" Offset="1"/>
                                    </LinearGradientBrush>
                                </Border.Background>
                                <Expander IsExpanded="{Binding Path=IsExpanded}" ExpandDirection="Right">
                                    <Expander.Header>
                                        <TextBlock x:Name="DecoratorElementName" FontWeight="SemiBold" FontFamily="Tahoma" FontSize="9pt" Margin="5,0,5,0" Grid.Column="1" Text="{Binding Path=DiagramTitle, Mode=OneWay}" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                                    </Expander.Header>
                                    <StackPanel MinHeight="600" MinWidth="300" Margin="0,0,0,100" Orientation="Vertical">
                                        <ContentControl Margin="0,-3,0,7" Visibility="Collapsed" x:Name="IncludedDCElement" Style="{StaticResource IncludedDCStyle}"/>

                                        <ContentControl Margin="0,-3,0,7" Visibility="Collapsed" x:Name="ImportedDCCElement" Style="{StaticResource ImportedDCCStyle}"/>

                                        <ItemsControl ItemsSource="{Binding Path=RootNodes}" ItemTemplateSelector="{StaticResource DiagramItemTemplateSelector}"/>
                                    </StackPanel>
                                </Expander>
                            </Border>
                        </Border>
                    </StackPanel>
                </StackPanel>
            </Grid>
        </ctrl:DesignerItem>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=HasImportedDCCItems}" Value="True">
                <Setter TargetName="ImportedDCCElement" Property="Visibility" Value="Visible"/>
            </DataTrigger>

            <DataTrigger Binding="{Binding Path=HasIncludedDCItems}" Value="True">
                <Setter TargetName="IncludedDCElement" Property="Visibility" Value="Visible"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>

    <DataTemplate DataType="{x:Type vm:DiagramClassViewModel}" x:Key="DependencyDiagramViewDefaultTemplate">
        <ctrl:DesignerItem x:Name="DiagramClassElement" Margin="-1,0,-1,0" HorizontalAlignment="Left">
            <Grid>
                <StackPanel Orientation="Horizontal">
                    <StackPanel>
                        <Border Margin="-1" Name="BdSelected" BorderBrush="Transparent" BorderThickness="1.2" Padding="1" SnapsToDevicePixels="True">
                            <Border Name="Bd" Margin="0" BorderBrush="#FF5D5A55" BorderThickness="0.6,0" Padding="0" SnapsToDevicePixels="True">
                                <Border.Background>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="White" Offset="0"/>
                                        <GradientStop Color="#22E2DBDB" Offset="1"/>
                                    </LinearGradientBrush>
                                </Border.Background>
                                <Expander IsExpanded="{Binding Path=IsExpanded}" ExpandDirection="Right">
                                    <Expander.Header>
                                        <TextBlock x:Name="DecoratorElementName" FontWeight="SemiBold" FontFamily="Tahoma" FontSize="9pt" Margin="5,0,5,0" Grid.Column="1" Text="{Binding Path=DiagramTitle, Mode=OneWay}" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                                    </Expander.Header>
                                    <StackPanel MinHeight="600" MinWidth="300" Margin="0,0,0,100" Orientation="Vertical">
                                        <ItemsControl ItemsSource="{Binding Path=RootNodes}" ItemTemplateSelector="{StaticResource DiagramItemTemplateSelector}"/>
                                    </StackPanel>
                                </Expander>
                            </Border>
                        </Border>
                    </StackPanel>
                </StackPanel>
            </Grid>
        </ctrl:DesignerItem>
    </DataTemplate>


    <selector:DiagramClassTemplateSelector x:Key="DiagramClassTemplateSelector"
                                           DefaultTemplate="{StaticResource DiagramClassViewDefaultTemplate}"
                                           DependencyDiagramTemplate="{StaticResource DependencyDiagramViewDefaultTemplate}"
                                           ModalDiagramTemplate ="{StaticResource ModalDiagramTemplate}"
                                           SpecificElementsDiagramTemplate ="{StaticResource SpecificElementsDiagramTemplate}"/>
    
    <!-- Diagram class element -->
    <Style TargetType="{x:Type ctrl:DesignerItem}" x:Key="DiagramClassElement" BasedOn="{StaticResource {x:Type ctrl:DesignerItem}}">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ctrl:DesignerItem}">
                    <ContentPresenter/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    <!-- Diagram Class Root Elements Template. -->
    <!--<DataTemplate x:Key="DiagramElementsTemplate">
        <ctrl:DesignerItem x:Name="DiagramClassElement" Margin="-1,0,-1,0" HorizontalAlignment="Left" Style="{StaticResource DiagramClassElement}"/>
    </DataTemplate>-->

    <!-- add scrolling to the itemscontrol -->
    <Style TargetType="{x:Type ItemsControl}" x:Key="DiagramItemsControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ItemsControl}">
                    <Grid>
                        <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                            <ctrl:DesignerCanvas SelectedItems="{Binding Path=SelectedItems, Mode=TwoWay}"
                                             ContextMenuOptions="{Binding Path=Operations, Mode=OneWay}"
                                             HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                                             Background="Transparent"
                                             Focusable="True">
                                <ctrl:DesignerCanvas.InputBindings>
                                    <KeyBinding Key="Delete" Command="{Binding Path=DeleteCommand}"/>
                                </ctrl:DesignerCanvas.InputBindings>
                                <ctrl:DesignerCanvas.ContextMenu>
                                    <ContextMenu ItemContainerStyleSelector="{StaticResource ContextMenuItemStyleSelector}"/>
                                </ctrl:DesignerCanvas.ContextMenu>
                                <ItemsPresenter Margin="0,0,1000,2000"/>
                            </ctrl:DesignerCanvas>
                        </ScrollViewer>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>